package itbour.onetouchshow.activity.searchlist;


import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.bean.VideoConditionBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.BroadcastAction;
import itbour.onetouchshow.utils.FinshBroadcast;
import itbour.onetouchshow.utils.SpUtils;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.DropDownViewPopup;

import static itbour.onetouchshow.base.BaseRefreshFragment.ON_REFRESH;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class SearchListActivity extends MVPBaseActivity<SearchListContract.View, SearchListPresenter> implements SearchListContract.View {

    private static final int FAST_CLICK_DELAY_TIME = 1000;
    @BindView(R.id.id_hedad1)
    public LinearLayout id_hedad1;
    @BindView(R.id.id_hedad2)
    public LinearLayout id_hedad2;
    @BindView(R.id.id_hedad3)
    public LinearLayout id_hedad3;
    @BindView(R.id.iv_search_back)
    protected ImageView ivSearchBack;
    @BindView(R.id.tv_homepage_hotsearch)
    protected TextView tvSearchTag;
    @BindView(R.id.rl_circular_shape)
    protected RelativeLayout rlCircularShape;
    @BindView(R.id.iv_search_list_back)
    protected ImageView backImage;
    protected SearchListFragment searchListFragment;
    protected String keywork;
    @BindView(R.id.searchContent)
    FrameLayout searchContent;
    @BindView(R.id.id_tab_layout)
    LinearLayout tabLayout;
    @BindView(R.id.et_hot_search)

    TextView tvSearchCancle;
    private VideoConditionBean conditionBean;
    private DropDownViewPopup modulePop;
    private DropDownViewPopup timeLongPop;
    private DropDownViewPopup timeSortPop;
    private List<VideoConditionBean.Option> moduleSortData;
    private List<VideoConditionBean.Option> timeLongSortData;
    private List<VideoConditionBean.Option> timeSortData;
    private long lastClickTime = 0L;
    //这是用于退出三段式逻辑的广播
    private FinshBroadcast quitThreeLogicBroadcast = new FinshBroadcast();

    @Override
    public void loadSucceed(String result) {
        searchListFragment.page.finishLoad(true);
    }

    @Override
    public void loadFaild(String error) {
        searchListFragment.page.finishLoad(false);
        showNoNetworkView();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
    }

    @Override
    public int getFragmentContentId() {
        return R.id.searchContent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_list;
    }

    @Override
    protected void afterCreate(Bundle savedInstanceState) {
        conditionBean = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class).getVideoConditions();

        if (mPresenter == null) {
            mPresenter = new SearchListPresenter(this);
        }
        // registerExitReceiver();
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            keywork = extras.getString("keywork");
        }
        tvSearchTag.setText(keywork);
        searchListFragment = new SearchListFragment();
        if (conditionBean != null) {
            if (conditionBean.getDurationCondition() != null && conditionBean.getDurationCondition().getOptions() != null) {
                searchListFragment.durations = new Gson().toJson(conditionBean.getDurationCondition().getOptions().get(0).getValues());
            }
            if (conditionBean.getTmplCondition() != null && conditionBean.getTmplCondition().getOptions() != null) {
                searchListFragment.tmplTypes = new Gson().toJson(conditionBean.getTmplCondition().getOptions().get(0).getValues());
            }
        }
        addFragment(searchListFragment);
        initFilterRule();
        ivSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                finish();
//                searchListFragment = null;
                onBackPressed();
            }
        });

        rlCircularShape.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                openActivity(MainActivity.class);
//                finish();
                //sendRemoveThreeLogic();
//                EventBus.getDefault().post("exit");
//                finish();
//                searchListFragment = null;

                onBackPressed();
            }
        });
    }

    private void sendRemoveThreeLogic() {
        Intent intent = new Intent();
        intent.setAction(BroadcastAction.EXIT_THREE_LOGIC);
        sendBroadcast(intent);
    }

    //onCreate调用
    private void registerExitReceiver() {
        IntentFilter exitFilter = new IntentFilter();
        exitFilter.addAction(BroadcastAction.EXIT_THREE_LOGIC);
        registerReceiver(quitThreeLogicBroadcast, exitFilter);
    }

    //onDestory反注册
    private void unRegisterExitReceiver() {

        unregisterReceiver(quitThreeLogicBroadcast);
    }


    @Override
    public void onBackPressed() {
        // finish();
        removeFragment();
        EventBus.getDefault().post("exit");
        //searchListFragment = null;
        super.onBackPressed();
    }

    /**
     * 弹出底部对话框
     */
    public void openModulePop() {
        if (moduleSortData == null) {
            moduleSortData = new ArrayList<>();
            moduleSortData.addAll(conditionBean.getTmplCondition().getOptions());
            if (moduleSortData.size() > 0) {
                moduleSortData.get(0).setSelected(true);
            }
        }
        TextView textView = id_hedad1.findViewById(R.id.selected_stand_status);
        ImageView imageView = id_hedad1.findViewById(R.id.chevron_image);
        modulePop = new DropDownViewPopup(SearchListActivity.this, moduleSortData, new DropDownViewPopup.ItemSelectListener() {
            @Override
            public void onSelect(VideoConditionBean.Option option, int position) {
                textView.setText(option.getName());
                searchListFragment.tmplTypes = new Gson().toJson(option.getValues());
                searchListFragment.pullStatus = ON_REFRESH;
                searchListFragment.page.loadPage(true);
            }
        });
        modulePop.showPopWindow(DropDownViewPopup.Location.left, tabLayout, imageView);
    }

    public void openTimeLongPop() {
        if (timeLongSortData == null) {
            timeLongSortData = new ArrayList<>();
            timeLongSortData.addAll(conditionBean.getDurationCondition().getOptions());
            if (timeLongSortData.size() > 0) {
                timeLongSortData.get(0).setSelected(true);
            }
        }
        TextView textView = id_hedad2.findViewById(R.id.selected_stand_status);
        ImageView imageView = id_hedad2.findViewById(R.id.chevron_image);
        timeLongPop = new DropDownViewPopup(SearchListActivity.this, timeLongSortData, new DropDownViewPopup.ItemSelectListener() {
            @Override
            public void onSelect(VideoConditionBean.Option option, int position) {
                textView.setText(option.getName());
                searchListFragment.durations = new Gson().toJson(option.getValues());
                searchListFragment.pullStatus = ON_REFRESH;
                searchListFragment.page.loadPage(true);
            }
        });
        timeLongPop.showPopWindow(DropDownViewPopup.Location.left, tabLayout, imageView);

    }

    public void openTimeSortPop() {
        if (timeSortData == null) {
            timeSortData = new ArrayList<>();
            timeSortData.add(new VideoConditionBean.Option("时间排序", null));
            timeSortData.add(new VideoConditionBean.Option("热度排序", null));
            if (timeSortData.size() > 0) {
                timeSortData.get(0).setSelected(true);
            }
        }
        TextView textView = id_hedad3.findViewById(R.id.selected_stand_status);
        ImageView imageView = id_hedad3.findViewById(R.id.chevron_image);
        timeSortPop = new DropDownViewPopup(SearchListActivity.this, timeSortData, new DropDownViewPopup.ItemSelectListener() {
            @Override
            public void onSelect(VideoConditionBean.Option option, int position) {
                textView.setText(option.getName());
                searchListFragment.orderBy = position + 1;
                searchListFragment.pullStatus = ON_REFRESH;
                searchListFragment.page.loadPage(true);
            }
        });
        timeSortPop.showPopWindow(DropDownViewPopup.Location.right, tabLayout, imageView);


//        modulePop = new DropDownViewPopup(getApplicationContext(), DropDownViewPopup.Location.left, mdata,
//                new DropDownViewPopup.ItemSelectListener() {
//                    @Override
//                    public void onSelect(String title, int position) {
//                        textView.setText(title);
//                        currentSelect1 = position;
//                        if (position == 1) {
//                            searchListFragment.mtype = TypePlatform.OBJECT_TYPE_VIDEO;
//                        } else if (position == 2) {
//                            searchListFragment.mtype = TypePlatform.OBJECT_TYPE_DESIGN;
//                        } else if (position == 3) {
//                            searchListFragment.mtype = TypePlatform.OBJECT_TYPE_PRINT;
//                        } else {
//                            searchListFragment.mtype = position;
//                        }
//                        //searchListFragment.mPageIndex=0;
//                        searchListFragment.pullStatus = ON_REFRESH;
//                        searchListFragment.page.loadPage(true);
//                    }
//                });
//
//        modulePop.showViewButtom(tabLayout, imageView);
//        modulePop.setCurrentpos(currentSelect1);
    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            // removeFragment();
////            finish();
////            searchListFragment = null;
//            onBackPressed();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }


    private void initFilterRule() {
        if (conditionBean.getTmplCondition() != null) {
            id_hedad1.setVisibility(View.GONE);
            TextView textTG1 = id_hedad1.findViewById(R.id.selected_stand_status);
            if (conditionBean.getTmplCondition().getOptions() != null && conditionBean.getTmplCondition().getOptions() != null && conditionBean.getTmplCondition().getOptions().size() > 0) {
                textTG1.setText(conditionBean.getTmplCondition().getOptions().get(0).getName());
            }
            textTG1.setTextColor(UIUtils.getColor(R.color.white));
        }

        if (conditionBean.getDurationCondition() != null) {
            id_hedad2.setVisibility(View.VISIBLE);
            TextView textTG2 = id_hedad2.findViewById(R.id.selected_stand_status);
            if (conditionBean.getDurationCondition().getOptions() != null && conditionBean.getDurationCondition().getOptions() != null && conditionBean.getDurationCondition().getOptions().size() > 0) {
                textTG2.setText(conditionBean.getDurationCondition().getOptions().get(0).getName());
            }

            textTG2.setTextColor(UIUtils.getColor(R.color.white));
        }


        TextView textTG3 = id_hedad3.findViewById(R.id.selected_stand_status);
        textTG3.setText("时间排序");
        textTG3.setTextColor(UIUtils.getColor(R.color.white));

        id_hedad1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME) {
                    return;
                }
                lastClickTime = System.currentTimeMillis();
                openModulePop();
            }
        });

        id_hedad2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME) {
                    return;
                }
                lastClickTime = System.currentTimeMillis();
                openTimeLongPop();
                // openModulePop();
            }
        });

        id_hedad3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (System.currentTimeMillis() - lastClickTime < FAST_CLICK_DELAY_TIME) {
                    return;
                }
                lastClickTime = System.currentTimeMillis();
                openTimeSortPop();
            }
        });
    }

}
